$(function() {
	$("#roomNum").keyup(
			function() {
				$.ajax({
					type : "POST",
					url : baseURL + "asset/select?roomNum="
							+ $("#roomNum").val(),
					success : function(r) {
						if (r.code === 0) {
							var str = "";
							$.each(r.menuList, function(i, n) {
								str += "<div class='div_item' data-id='"
										+ n.assetId + "' data-name='"
										+ n.roomNum + "'>" + n.roomNum
										+ "</div>"
							});
							$("#div_items1").html(str);
							$("#div_items1").show();

							$(".div_item").unbind("click").bind(
									"click",
									function() {
										$("#roomNum").val(
												$(this).attr("data-name"));
										$("#roomNumQuery").val(
												$(this).attr("data-id"));
										vm.q.roomNum = $(this).attr("data-id");
										$("#div_items1").hide();
									});

						} else {
							alert(r.msg);
						}
					}
				});
			});
	$("#roomNum2")
			.keyup(
					function() {
						$
								.ajax({
									type : "POST",
									url : baseURL + "asset/select?roomNum="
											+ $("#roomNum2").val(),
									success : function(r) {
										if (r.code === 0) {
											var str = "";
											$
													.each(
															r.menuList,
															function(i, n) {
																str += "<div class='div_item' data-id='"
																		+ n.assetId
																		+ "' data-name='"
																		+ n.roomNum
																		+ "'>"
																		+ n.roomNum
																		+ "</div>"
															});
											$("#div_items").html(str);
											$("#div_items").show();

											$(".div_item")
													.unbind("click")
													.bind(
															"click",
															function() {
																$exist = $(
																		"#roomNumList")
																		.find(
																				"span[data-id='"
																						+ $(
																								this)
																								.attr(
																										"data-id")
																						+ "']");
																if ($exist.length > 0) {
																	alert("该房号已选择");
																	return;
																}
																var str = "<span class='room' data-id='"
																		+ $(
																				this)
																				.attr(
																						"data-id")
																		+ "'>"
																		+ $(
																				this)
																				.attr(
																						"data-name")
																		+ "<i class='close'>×</i></span>";
																$(
																		"#roomNumList")
																		.append(
																				str);
																$("#roomNums")
																		.val(
																				$(
																						"#roomNums")
																						.val()
																						+ $(
																								this)
																								.attr(
																										"data-id")
																						+ ";");
																$("#div_items")
																		.hide();
																$("#roomNum2")
																		.val("");

																$(".close")
																		.unbind(
																				"click")
																		.bind(
																				"click",
																				function() {
																					$(
																							this)
																							.closest(
																									"span")
																							.remove();
																					var room = "";
																					$
																							.each(
																									$(".room"),
																									function(
																											i,
																											n) {
																										room += $(
																												n)
																												.attr(
																														"data-id")
																												+ ";";
																									});
																					$(
																							"#roomNums")
																							.val(
																									room);
																				});
															});

										} else {
											alert(r.msg);
										}
									}
								});
					});
	$("#roomNum2,#roomNum").unbind('keydown').bind('keydown', function(e) {
		var keyCode = e.keyCode;
		if (keyCode == 9) {
			$("#div_items,#div_items1").hide();
		}
	});
	$(document).unbind('mouseup.parentCls').bind(
			'mouseup.parentCls',
			function(e) {
				var $target = $(e.target);
				if ($target.is('[disabled]') || $target.is('[disabled="true"]')
						|| $target.is('[readonly]')
						|| $target.is('[readonly="readonly"]')) {
					return true;
				}
				if ($(e.target).closest('.parentCls').length == 0) {
					$("#div_items,#div_items1").hide();
				}
			});
	$("#roomNum2").focus(function() {
		if ($("#roomNum2").val()) {
			$("#div_items").show();
		}
	})

	new AjaxUpload('#upload', {
		action : baseURL + 'comimage/upload?token=' + token,
		name : 'file',
		autoSubmit : true,
		responseType : "json",
		onSubmit : function(file, extension) {

			if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension
					.toLowerCase()))) {
				alert('只支持jpg、png、gif格式的图片！');
				return false;
			}
		},
		onComplete : function(file, r) {
			if (r.code == 0) {
				var imageIds = $("#imageIds").val();
				$.get(baseURL + "comimage/getPhoto?imageId=" + r.imageId,
						function(v) {
							imageIds += r.imageId + ";";
							$("#imageIds").val(imageIds);
							vm.comContract.imageIds = imageIds;
							$("#imageList").append(
									'<img width="100" height="150" src="data:image/png;base64,'
											+ v.imageStr
											+ '" data-action="zoom"/>');
						});
			} else {
				alert(v.msg);
			}
		}
	});
});

var str = ""; // 这是一字符串
var vm = new Vue({
	el : '#rrapp',
	data : {
		comContract : {
			leasebackFlag : 1,
			suppleFlag : 1,
			imageIds : "",
			imageIds1 : "",
			startDate : "",
			effectDate : "",
			endDate : "",
			salesDate : ""
		}
	},
	methods : {
		add : function() {
			vm.comContract = {
				leasebackFlag : 1,
				suppleFlag : 1,
				imageIds : "",
				startDate : "",
				endDate : "",
				effectDate : "",
				salesDate : ""
			};
			vm.getMenu();
			$("#imageList").html("");
			$('#startDate').val("");
			$('#endDate').val("");
			$('#effectDate').val("");
			$('#salesDate').val("");
			$('#roomNums').val("");
			$('#roomNumList').empty();

		},
		back : function(event) {
			parent.setSrc("modules/com/contract.html");
		},
		saveOrUpdate : function(event) {
			var url = vm.comContract.contractId == null ? "comcontract/save"
					: "comcontract/update";
			vm.comContract.assetIds = $("#roomNums").val();
			$.ajax({
				type : "POST",
				url : baseURL + url,
				data : JSON.stringify(vm.comContract),
				success : function(r) {
					if (r.code === 0) {
						alert('操作成功', function(index) {
							vm.back();
						});
					} else {
						alert(r.msg);
					}
				}
			});
		}
	}
});

$(function() {
	$('#startDate').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				$('#startDate').text("");
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				var day = date.getDate();
				if (day < 10) {
					day = '0' + day;
				}
				$('#startDate').val(year + '-' + month + '-' + day);
				vm.comContract.startDate = year + '-' + month + '-' + day;
			});

	$('#endDate').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				$('#endDate').text("");
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				var day = date.getDate();
				if (day < 10) {
					day = '0' + day;
				}
				$('#endDate').val(year + '-' + month + '-' + day);
				vm.comContract.endDate = year + '-' + month + '-' + day;
			});
	$('#endDate2').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				$('#endDate2').text("");
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				var day = date.getDate();
				if (day < 10) {
					day = '0' + day;
				}
				$('#endDate2').val(year + '-' + month + '-' + day);
				vm.comContract.endDate = year + '-' + month + '-' + day;
			});
	$('#effectDate').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				$('#effectDate').text("");
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				var day = date.getDate();
				if (day < 10) {
					day = '0' + day;
				}
				$('#effectDate').val(year + '-' + month + '-' + day);
				vm.comContract.effectDate = year + '-' + month + '-' + day;
			});
	$('#effectDate2').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				$('#effectDate2').text("");
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				var day = date.getDate();
				if (day < 10) {
					day = '0' + day;
				}
				$('#effectDate2').val(year + '-' + month + '-' + day);
				vm.comContract.effectDate = year + '-' + month + '-' + day;
			});
	$('#endDateS').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				$('#endDateS').text("");
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				var day = date.getDate();
				if (day < 10) {
					day = '0' + day;
				}
				$('#endDateS').val(year + '-' + month + '-' + day);
				vm.q.endDateS = year + '-' + month + '-' + day;
			});
	$('#endDateE').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				$('#endDateE').text("");
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				var day = date.getDate();
				if (day < 10) {
					day = '0' + day;
				}
				$('#endDateE').val(year + '-' + month + '-' + day);
				vm.q.endDateE = year + '-' + month + '-' + day;
			});
	$('#salesDate').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				$('#salesDate').text("");
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				var day = date.getDate();
				if (day < 10) {
					day = '0' + day;
				}
				$('#salesDate').val(year + '-' + month + '-' + day);
				vm.comContract.salesDate = year + '-' + month + '-' + day;
			});

	$('#endDateE').keyup(function(e) {
		if (e.keyCode == 8) {
			$('#endDateE').val();
			$('#endDateE').text("");
			vm.q.endDateE = "";
		}
	});
	$('#endDateS').keyup(function(e) {
		if (e.keyCode == 8) {
			$('#endDateS').val();
			$('#endDateS').text("");
			vm.q.endDateS = "";
		}
	});

})